#include "StdAfx.h"
#include "Shell.h"
#include "resource.h"

CShell::CShell(void)
	: connected(false), hostname(NULL), port(0)
{
}

CShell::~CShell(void)
{
	delete this->hostname;
}

void CShell::SetHostname(LPCTSTR hostname)
{
	ASSERT(hostname != NULL);
	ASSERT(this->hostname == NULL);
	ASSERT(!this->connected);
	this->hostname = _tcsdup(hostname);
}

void CShell::SetPort(int port)
{
	ASSERT(this->port == 0);
	ASSERT(port > 0);
	ASSERT(!this->connected);
	this->port = port;
}

void CShell::Connect()
{
	ASSERT(this->port > 0);
	ASSERT(this->hostname != NULL);
	ASSERT(_tcslen(this->hostname) > 0);

	Create();
	CAsyncSocket::Connect(this->hostname, this->port);
}

void CShell::Disconnect()
{
	CAsyncSocket::ShutDown();
	CAsyncSocket::Close();
	this->connected = false;

	OnDisconnectEvent();
}

bool CShell::IsConnected()
{
	return this->connected;
}

void CShell::OnConnect(int nErrorCode)
{
	this->connected = (nErrorCode == 0);
	OnConnectEvent(nErrorCode);
}